package com.nit.lab.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.nit.lab.dto.system.RoleDTO;
import com.nit.lab.dto.utils.OpResultDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.service.system.RoleService;
import com.nit.lab.utils.HttpRequestReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/api/role")
public class RoleController {

    @Resource
    private RoleService roleService;
    private final Logger logger = LoggerFactory.getLogger(RoleController.class);

    /**
     * @Description: 获取数据列表
     * @Author: SN
     * @Date: 2020/02/30 18:15
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public TableRspDTO list4Table(@RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            tableRspDTO = roleService.list4Table(tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    /**
     * @Description: 新增记录
     * @Author: SN
     * @Date: 2020/02/30 18:15
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestBody RoleDTO roleDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(roleService.add(roleDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 更新记录
     * @Author: SN
     * @Date: 2020/02/30 18:15
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestBody RoleDTO roleDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(roleService.update(roleDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 删除记录
     * @Author: SN
     * @Date: 2020/02/30 18:15
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public OpResultDTO remove(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String roleIds = jsonRequest.getString("roleIds");
            opResult.setMessage(roleService.remove(roleIds).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 获取角色信息用于选择
     * @Author: SN
     * @Date: 2020/01/30 18:15
     */
    @RequestMapping(value = "/listRole", method = RequestMethod.POST)
    public OpResultDTO listRole() {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage("success");
            opResult.setResult(roleService.listRole());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 根据角色ID，获取权限信息，返回字符串形式
     * @Author: SN
     * @Date: 2020/02/30 18:15
     */
    @RequestMapping(value = "/listPermissionByRoleId4Tree", method = RequestMethod.POST)
    OpResultDTO listPermissionByRoleId(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            Long roleId = Long.parseLong(jsonRequest.getString("roleId"));
            opResult.setMessage("success");
            opResult.setResult(roleService.listPermissionByRoleId4Tree(roleId));
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 获取全部权限信息
     * @Author: SN
     * @Date: 2020/02/30 18:15
     */
    @RequestMapping(value = "/listPermission4Tree", method = RequestMethod.POST)
    public OpResultDTO listPermission4Tree() {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage("success");
            opResult.setResult(roleService.listPermission4Tree());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 角色授权
     * @Author: SN
     * @Date: 2020/02/30 18:15
     */
    @RequestMapping(value = "/authorize", method = RequestMethod.POST)
    public OpResultDTO authorize(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            Long roleId = Long.parseLong(jsonRequest.getString("roleId"));
            String menuIds = jsonRequest.getString("menuIds");
            opResult.setMessage(roleService.authorize(roleId, menuIds).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
}
